package com.wwwarehouse.contract.contract.modify;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.contract.bean.InfoCardOrderDetailBean;
import com.wwwarehouse.contract.contract.TheParentFragment;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.core.ContractEventCode;
import com.wwwarehouse.contract.core.EventManager;
import com.wwwarehouse.contract.event.ContractEvent;
import com.wwwarehouse.contract.event.DeleteIssuedGoodsEvent;

import java.util.ArrayList;
import java.util.List;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * @author feng.gao on 2017/11/8.
 *         <p/>
 *         已发布商品
 */

public class IssuedGoodsFragment extends TheParentFragment {

	private RecyclerView mRvContent;
	private ArrayList<InfoCardOrderDetailBean.ItemDetailBean> mItemList = new ArrayList<>();
	IssuedGoodsAdapter mAdapter;
	private PopupWindow mPopupWindow;

	@Override
	protected int getContentId() {
		return R.layout.contract_issued_goods_list_fragment;
	}

	@Override
	public void setTitle() {

	}

	@Override
	protected void init(View view) {
		mRvContent = findView(view, R.id.rv_content);
	}

	@Override
	public void onSuccess(CommonClass commonClass, int what) {

	}

	@Override
	public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
		if (!enter) {
			return null;
		}
		if (getArguments() == null || getArguments().getParcelableArrayList("dataList") == null) {
			mLoadingView.setVisibility(View.VISIBLE);
			showEmptyView();
			mLoadingView.setEmptyListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					requestDatas();
				}
			});
			return null;
		}

		final ArrayList<InfoCardOrderDetailBean.ItemDetailBean> listBean = getArguments().getParcelableArrayList("dataList");
		if (listBean == null || listBean.size() == 0) {
			showEmptyView();
		}
		int position = getArguments().getInt("page") - 1;
		int pageNum = 5;

		mItemList.clear();
		for (int i = position * pageNum; i < (position + 1) * pageNum; i++) {
			if (i >= 0 && i < listBean.size()) {
				mItemList.add(listBean.get(i));
			}
		}
		mRvContent.setLayoutManager(new GridLayoutManager(mActivity, 1) {
			@Override
			public boolean canScrollHorizontally() {
				return false;
			}

			@Override
			public boolean canScrollVertically() {
				return false;
			}

		});
		final ViewTreeObserver observer = mRvContent.getViewTreeObserver();
		observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
			@Override
			public void onGlobalLayout() {
				observer.removeGlobalOnLayoutListener(this);
				if (mAdapter == null) {
					mAdapter = new IssuedGoodsAdapter(mActivity, mItemList, mRvContent.getHeight());
					mAdapter.setPopUPWindowListener(new IssuedGoodsAdapter.showPopListener() {
						@Override
						public void showPopWindow(View view, int position) {
							shwoPopupWindow(view , position);
						}
					});
					mAdapter.setOnClickRuleListener(new IssuedGoodsAdapter.clickRuleListener() {
						@Override
						public void RuleClick(int position) {
							//ReleaseAddingPublishingRulesFragment
							Bundle bundle = new Bundle();
							bundle.putParcelable("data" , mItemList.get(position));
							ModifyRulesFragment modifyRulesFragment = new ModifyRulesFragment();
							modifyRulesFragment.setArguments(bundle);
							pushFragment(modifyRulesFragment);
						}
					});
				}
				mRvContent.setAdapter(mAdapter);
			}
		});
		return null;
	}

	@Override
	public void requestDatas() {


	}

	private void showEmptyView() {
		mLoadingView.showEmptyView(true);
		mLoadingView.setEmptyListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				requestDatas();
			}
		});
	}


	@Override
	public void onReLoad(int code) {

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
		if (mPopupWindow!=null&&mPopupWindow.isShowing()){
			mPopupWindow.dismiss();
		}
	}

	private void shwoPopupWindow(View view, final int position) {
		int[] location = new int[2];
		view.getLocationInWindow(location);
		View popupView = View.inflate(mActivity, R.layout.delete_item_popupwindow_layout, null);
		mPopupWindow = new PopupWindow(popupView, view.getWidth(), view.getHeight() - 20);
		Drawable drawable = new ColorDrawable(Color.TRANSPARENT);
		LinearLayout mDeleteLayout = (LinearLayout) popupView.findViewById(R.id.delete_layout);
		mDeleteLayout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				EventManager.post(new ContractEvent<DeleteIssuedGoodsEvent>(ContractEventCode.CODE_DELETE_ISSUED_GOODS , new DeleteIssuedGoodsEvent(mItemList.get(position).getItemPublishUkid())));
				mPopupWindow.dismiss();

			}
		});
		mPopupWindow.setBackgroundDrawable(drawable);
		mPopupWindow.setOutsideTouchable(true);
		int x = location[0];
		int y = location[1];
		mPopupWindow.showAtLocation(view, Gravity.LEFT | Gravity.TOP, x, y);
	}
}
